/**
 * 09/dic/2011 - fabio.melillo@gmail.com
 */
package beta.unisannio.fm.interactive;

import org.unisannio.remote.RemoteBroadcaster;

import beta.unisannio.fm.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * @author fabio
 *
 */
public class ConfigActivity extends Activity {
	
	private RemoteBroadcaster broadRemote;
	
	ToggleButton registraSuperPeer;
	TextView tvSuperPeer;
	TextView editSuperPeerAddress;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.config);
		registraSuperPeer=(ToggleButton) findViewById(R.id.toggleSuperpeer);
		tvSuperPeer=(TextView) findViewById(R.id.tvSuperPeerAddress);
		editSuperPeerAddress=(TextView) findViewById(R.id.etSuperPeerAddress);
		
		SharedPreferences pref=getSharedPreferences(HelpMeActivity.preferenceName, 0);
        String superpeerAddress=pref.getString(HelpMeActivity.superpeer, null);
		
        if (superpeerAddress!=null) {
        	editSuperPeerAddress.setText(superpeerAddress);
        }
        
		registraSuperPeer.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String superString = editSuperPeerAddress.getText().toString();
				if (superString==null || superString.length()<=0)
					{
					AlertDialog.Builder builder = new AlertDialog.Builder(ConfigActivity.this);
					builder.setMessage("Insert SuperPeer address");
					builder.setCancelable(false);
					builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					               dialog.cancel();
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
					registraSuperPeer.setChecked(false);
					}
				else{
					HelpMeActivity.remoteBroadcaster.setServer(superString);
					Editor prefEditor=getSharedPreferences(HelpMeActivity.preferenceName, 0).edit();
					prefEditor.putString(HelpMeActivity.superpeer, superString);
					prefEditor.commit();
					tvSuperPeer.setText(HelpMeActivity.remoteBroadcaster.getServerAddress().toString());
				}
				
			}
		});
		
		
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (HelpMeActivity.remoteBroadcaster==null){
			this.startActivity(new Intent(this, HelpMeActivity.class));
		}
		else{
			if (HelpMeActivity.remoteBroadcaster.getServerAddress()!=null){
				registraSuperPeer.setChecked(true);
				tvSuperPeer.setText(HelpMeActivity.remoteBroadcaster.getServerAddress().toString());
				editSuperPeerAddress.setText(HelpMeActivity.remoteBroadcaster.getServerAddress().toString().substring(4));
			}
			else{
				registraSuperPeer.setChecked(false);
				tvSuperPeer.setText("No superpeer set");
			}
		}
	}
	
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		setResult(RESULT_OK,null);
	}
}
